package com.sxbbc.common.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxbbc.common.core.entity.jz.WUser;
import com.sxbbc.common.core.mapper.jz.WUserMapper;
import com.sxbbc.common.core.service.IWRolesService;
import com.sxbbc.common.core.service.IWUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxbbc.common.core.utils.BasePage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 仓库用户表 服务实现类
 * </p>
 *
 * @author leehao
 * @since 2019-07-01
 */
@Service
public class WUserServiceImpl extends ServiceImpl<WUserMapper, WUser> implements IWUserService {

    @Autowired
    private IWRolesService iwRolesService;

    @Override
    public WUser selectByLogin(String login) {
        QueryWrapper<WUser> wrapper = new QueryWrapper<>();
        wrapper.eq("login",login);
        return super.getOne(wrapper);
    }

    @Override
    public BasePage<WUser> queryList(BasePage<WUser> page, WUser wUser) {
        QueryWrapper<WUser> wrapper = queryWrapper(wUser);
        if (page.isPaging()) {
            super.page(page,wrapper);
        }else{
            List<WUser> list = super.list(wrapper);
            page.setRecords(list);
        }
        for (WUser w : page.getRecords()) {
            w.setRoleName(iwRolesService.getById(w.getRoleId()).getName());
        }
        return page;
    }

    private QueryWrapper<WUser> queryWrapper(WUser wUser) {
        QueryWrapper<WUser> wrapper = new QueryWrapper<>();
        wrapper.eq("wid",wUser.getWid());
        if (StringUtils.isNotBlank(wUser.getLogin())) {
            wrapper.eq("login",wUser.getLogin());
        }
        if (StringUtils.isNotBlank(wUser.getName())) {
            wrapper.eq("name",wUser.getName());
        }
        return wrapper;
    }
}
